# Maintainer: Michael Hansen <zrax0111 gmail com>

_realname=libvncserver
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.15
pkgrel=1
pkgdesc="Cross-platform C libraries that allow you to easily implement VNC server or client functionality (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libvnc.github.io/"
msys2_repository_url="https://github.com/LibVNC/libvncserver"
msys2_references=(
  "cpe: cpe:/a:libvnc_project:libvncserver"
  "cpe: cpe:/a:libvncserver:libvncserver"
  "cpe: cpe:/a:libvncserver_project:libvncserver"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-winpthreads-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-libssh2"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-SDL2")
source=("https://github.com/LibVNC/libvncserver/archive/LibVNCServer-${pkgver}.tar.gz")
sha256sums=('62352c7795e231dfce044beb96156065a05a05c974e5de9e023d688d8ff675d7')

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_SHARED_LIBS=ON \
    -DWITH_EXAMPLES=OFF \
    -DWITH_TESTS=OFF \
    ../${_realname}-LibVNCServer-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  # Fix cmake files
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
      -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;
}
